package com.chensheng.service;

import com.chensheng.dto.OrderDTO;
import com.chensheng.enums.OrderStatusEnum;
import com.chensheng.enums.PayStatusEnum;
import com.chensheng.enums.ResultErrorEnum;
import com.chensheng.exception.SellException;
import com.chensheng.model.OrderMaster;
import com.chensheng.util.JsonUtil;
import com.chensheng.util.MathUtil;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:ChenSheng
 * @Description:
 * @Date: Created in 下午 22:36 2018/5/16 0016
 */
@Service
@Slf4j
public class PayService {

    private static final String ORDER_NAME = "微信点餐订单";
    private static final String J_OPENID = "oTgZpwYGFuZKabSCelbu-cOI_Nb0";

    @Autowired
    private BestPayServiceImpl bestPayService;

    @Autowired
    private OrderMasterService orderMasterService;

    @Autowired
    private OrderService orderService;

    public PayResponse create(OrderMaster orderMaster){
        PayRequest payRequest = new PayRequest();
        //payRequest.setOpenid(J_OPENID);
        payRequest.setOpenid(orderMaster.getBuyerOpenid());
        payRequest.setOrderAmount(orderMaster.getOrderAmount().doubleValue());
        payRequest.setOrderId(orderMaster.getOrderId());
        //payRequest.setOrderAmount(3.16);
        //payRequest.setOrderId("15264794630678257437");
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信支付】request = {}", JsonUtil.toJson(payRequest));


        PayResponse payResponse = bestPayService.pay(payRequest);
        log.info("【微信支付】response = {}", JsonUtil.toJson(payResponse));
        return payResponse;
    }

    public PayResponse notify(String notifyData){
        //验证签名
        //支付的状态
        //支付金额
        //支付人（下单人==支付人）

        PayResponse payResponse = bestPayService.asyncNotify(notifyData);
        log.info("【微信支付】异步通知，payResponse={}", JsonUtil.toJson(payResponse));

        OrderMaster orderMaster = orderMasterService.findOneByOrderId(payResponse.getOrderId());
        if(orderMaster == null){
            log.error("【微信支付】异步通知，订单不存在，orderId={}", payResponse.getOrderId());
            throw new SellException(ResultErrorEnum.ORDER_NOT_EXIST_ERROR);
        }

        //判断金额是否一致
        if(!MathUtil.equals(orderMaster.getOrderAmount().doubleValue(), payResponse.getOrderAmount())){
            log.error("【微信支付】异步通知，订单金额不一致, orderId={}, 微信通知金额={}， 系统金额={}",
                    payResponse.getOrderAmount(), orderMaster.getOrderAmount());
            throw new SellException(ResultErrorEnum.WXPAY_NOTIFY_MONEY_VERIFY_ERROR);
        }

        //修改订单的支付状态
        //orderMasterService.setPayStatus(PayStatusEnum.PAYED.getCode(), orderMaster.getOrderId());
        orderService.paid(orderMaster);

        return payResponse;
    }

    public RefundResponse refund(OrderMaster orderDTO){
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);

        log.info("【微信退款】request={}", JsonUtil.toJson(refundRequest));

        RefundResponse refundResponse = bestPayService.refund(refundRequest);
        log.info("【微信退款】response={}", JsonUtil.toJson(refundResponse));

        return refundResponse;
    }


}
